package com.twang.stock.controller.old;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.twang.stock.dto.GaiNianZiJingPaiHangDto;
import com.twang.stock.enmu.HotTagEnum;
import com.twang.stock.entity.*;
import com.twang.stock.service.impl.MinutesVolRuleService;
import com.twang.stock.service.jinyan.*;
import com.twang.stock.task.old.QinXUZhiShuTask;
import com.twang.stock.task.old.StockMoneyInTask;
import com.twang.stock.utils.date.HolidayCopy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/stock")
@Slf4j
public class StockController {

//    @Autowired
//    private QueryDataService queryDataService;

    @Autowired
    private StockInfoService stockInfoService;

    @Autowired
    private MinutesVolRuleService minutesVolRuleService;
    @Autowired
    private GetStockListService getStockListService;


    @Autowired
    private StockMoneyInListService stockMoneyInListService;
    @Resource
    private StockMoneyInTask stockMoneyInTask;

    @Autowired
    private CompanyCodeInfoService companyCodeInfoService;
    @Autowired
    private UpdateAndFixDataService updateAndFixDataService;

    @Autowired
    private UpdtePriceAndVolService updtePriceAndVolService;

    @Autowired
    private QueryBanKuaiRateService queryBanKuaiRateService;

    @Autowired
    private DuanXianBaoXuanGuService duanXianBaoXuanGuService;

    @Autowired
    private  UpdateStockCoreService updateStockCoreService;

    @Autowired
    private JiSuanZhangDieService jiSuanZhangDieService;

    @Autowired
    private QinXUZhiShuService qinXUZhiShuService;
    @Autowired
    private HolidayService holidayService;
    @Autowired
    private QinXUZhiShuTask qinXUZhiShuTask;

    /**
     * 获取数据
     *
     * @return
     */
    @RequestMapping(value = "/query")
    public ModelAndView query(String stockName) {
        ModelAndView modelAndView = new ModelAndView();
        List list = Lists.newArrayList();
        try {
            //1、热度
           // list = queryDataService.select();
            if (StringUtils.isNotEmpty(stockName)) {
                //2、主力资金流入
                {
                    if("000001".equals(stockName)){
                        List<HotTag> qinXuZhiShu = qinXUZhiShuService.queryListByTag(HotTagEnum.CORE_DUAN_XIAN_BAO);
                        modelAndView.addObject("stockName", "短线收益曲线");
                        modelAndView.addObject("stockMoneyInList", qinXuZhiShu);
                    }else {
                        List<StockMoneyIn> stockMoneyInList = stockMoneyInListService.getMoneyInByStockName(stockName);
                        modelAndView.addObject("stockName", companyCodeInfoService.getInfoByStockName(stockName).getShortName());
                        modelAndView.addObject("stockMoneyInList", stockMoneyInList);
                    }

                }
                // 3、概念板块排行
                {
                    //当日
                    List<GaiNianZiJingPaiHangDto> listDto = queryBanKuaiRateService.queryBanKuaiRateService();
                    modelAndView.addObject("banKuaiRate", listDto);
                    //作日
                    List<GaiNianZiJingPaiHangDto> yesterday = queryBanKuaiRateService.queryBanKuaiRateYesterdayService();
                    modelAndView.addObject("yesterdayRate", yesterday);
                    //作日
                    List<GaiNianZiJingPaiHangDto> beforeYesterday = queryBanKuaiRateService.queryBanKuaiRateBeforeYesterdayService();
                    modelAndView.addObject("beforeYesterdayRate", beforeYesterday);
                }
                //4、热点强势
                {
                    List<DuanXianBao> duanXianBaoList=  duanXianBaoXuanGuService.queryHotStock(HolidayCopy.getRealToday());
                    modelAndView.addObject("duanXianBaoList", duanXianBaoList);
                }
                //情绪指数
                {
                    List<String> qingxuDate=Lists.newArrayList();
                    List<BigDecimal> qingxuZhiShuYesterday=Lists.newArrayList();
                    List<BigDecimal> qingxuZhiShuToday=Lists.newArrayList();
                    List<QinXuZhiShu> qinXuZhiShuList=qinXUZhiShuService.getZhiShu();
                    List<QinXuZhiShu> 新能源=qinXuZhiShuList.stream().filter(item->item.getName().equals(HotTagEnum.Core_1.getMessage())).collect(Collectors.toList());
                    List<QinXuZhiShu> 医美=qinXuZhiShuList.stream().filter(item->item.getName().equals(HotTagEnum.Core_2.getMessage())).collect(Collectors.toList());
                    List<QinXuZhiShu> 白酒=qinXuZhiShuList.stream().filter(item->item.getName().equals(HotTagEnum.Core_3.getMessage())).collect(Collectors.toList());
                    List<QinXuZhiShu> 核心=qinXuZhiShuList.stream().filter(item->item.getName().equals(HotTagEnum.Core_4.getMessage())).collect(Collectors.toList());
                    List<QinXuZhiShu> 情绪连板=qinXuZhiShuList.stream().filter(item->item.getName().equals(HotTagEnum.Core_6.getMessage())).collect(Collectors.toList());

                    for(int i=0;i<11;i++) {
                        if (0 == i) {
                            qingxuDate.add("新能源:"+新能源.get(0).qingxuName);
                            qingxuZhiShuYesterday.add(新能源.get(1).getZhiShu().compareTo(BigDecimal.ZERO)==0?BigDecimal.ONE:新能源.get(1).getZhiShu());
                            qingxuZhiShuToday.add(新能源.get(0).getZhiShu().compareTo(BigDecimal.ZERO)==0?BigDecimal.ONE:新能源.get(0).getZhiShu());
                        }
                        if (1 == i) {
                            qingxuDate.add("医美:"+医美.get(0).qingxuName);
                            qingxuZhiShuYesterday.add(医美.get(1).getZhiShu().compareTo(BigDecimal.ZERO)==0?BigDecimal.ONE:医美.get(1).getZhiShu());
                            qingxuZhiShuToday.add(医美.get(0).getZhiShu().compareTo(BigDecimal.ZERO)==0?BigDecimal.ONE:医美.get(0).getZhiShu());
                        }
                        if (2 == i) {
                            qingxuDate.add("白酒:"+白酒.get(0).qingxuName);
                            qingxuZhiShuYesterday.add(白酒.get(1).getZhiShu().compareTo(BigDecimal.ZERO)==0?BigDecimal.ONE:白酒.get(1).getZhiShu());
                            qingxuZhiShuToday.add(白酒.get(0).getZhiShu().compareTo(BigDecimal.ZERO)==0?BigDecimal.ONE:白酒.get(0).getZhiShu());
                        }
                        if (3 == i) {
                            qingxuDate.add("核心:"+核心.get(0).qingxuName);
                            qingxuZhiShuYesterday.add(核心.get(1).getZhiShu().compareTo(BigDecimal.ZERO)==0?BigDecimal.ONE:核心.get(1).getZhiShu());
                            qingxuZhiShuToday.add(核心.get(0).getZhiShu().compareTo(BigDecimal.ZERO)==0?BigDecimal.ONE:核心.get(0).getZhiShu());
                        }
                        if (4 == i) {
                            qingxuDate.add("情绪连板:" + 情绪连板.get(0).qingxuName);
                            qingxuZhiShuYesterday.add(情绪连板.get(1).getZhiShu().compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : 情绪连板.get(1).getZhiShu());
                            qingxuZhiShuToday.add(情绪连板.get(0).getZhiShu().compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : 情绪连板.get(0).getZhiShu());
                        }
                    }
                    modelAndView.addObject("qingxuDate", qingxuDate);
                    modelAndView.addObject("qingxuZhiShuYesterday", qingxuZhiShuYesterday);
                    modelAndView.addObject("qingxuZhiShuToday", qingxuZhiShuToday);
                }
                //板块效应
                {
                    List<GaiNianZiJingPaiHang> gaiNianZiJingPaiHangRatio = queryBanKuaiRateService.查询板块by涨跌比();
                    gaiNianZiJingPaiHangRatio.forEach(item -> {
                        item.setRatio(item.getRatio().multiply(new BigDecimal(100)));
                    });
                    gaiNianZiJingPaiHangRatio= gaiNianZiJingPaiHangRatio.stream().sorted(Comparator.comparing(GaiNianZiJingPaiHang::getZhangDie)).collect(Collectors.toList());
                    List<String> ratioNameList = gaiNianZiJingPaiHangRatio.stream().map(GaiNianZiJingPaiHang::getHanYe).collect(Collectors.toList());
                    List<BigDecimal> ratioList = gaiNianZiJingPaiHangRatio.stream().map(GaiNianZiJingPaiHang::getRatio).collect(Collectors.toList());
                    modelAndView.addObject("ratioNameList", ratioNameList);
                    modelAndView.addObject("ratioList", ratioList);
                }
                //短线宝
                {
                    QinXuZhiShu qinXuZhiShu = qinXUZhiShuService.duanxian().get(0);
                    List<StockInfo> qinXuZhiShuList = stockInfoService.queryByPage(null);
                    BigDecimal rate = BigDecimal.ZERO;
                    for (StockInfo info : qinXuZhiShuList) {
                        rate = rate.add(info.getZhangdie());
                    }
                    if(qinXuZhiShuList.size()>0){
                        rate = rate.divide(new BigDecimal(qinXuZhiShuList.size()), 2, BigDecimal.ROUND_HALF_UP);
                    }
                    modelAndView.addObject("qinXuZhiShuList", qinXuZhiShuList);
                    modelAndView.addObject("qinXuZhiShu", qinXuZhiShu);
                    modelAndView.addObject("rate", rate);
                }
            }
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        modelAndView.setViewName("stock");
        modelAndView.addObject("list", list);
        return modelAndView;
    }

    @RequestMapping(value = "/insert")
    public String insert() {
        List list = Lists.newArrayList();
        try {
           // list = queryDataService.select();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }

    @RequestMapping(value = "/queryCode")
    public String queryList() {
        List list = Lists.newArrayList();
        try {
          // list = stockInfoService.queryList(null);
            minutesVolRuleService.excute(1200000,789000,"sz000799",159);
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }

    /**
     * http://localhost:8080/stock/queryHistory?startDate=2021-03-01&endDate=2021-03-30&code=002340
     * @param startDate
     * @param endDate
     * @param code
     * @return
     */
    @RequestMapping(value = "/queryHistory")
    public String queryHistory(String startDate,String endDate,String code) {
        List list = Lists.newArrayList();
        try {
            // list = stockInfoService.queryList(null);
            stockInfoService.queryHistoryData(startDate,endDate,code);
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }

    @RequestMapping(value = "/updateVol")
    public String updateVol(String startDate,String endDate,String code) {
        List list = Lists.newArrayList();
        try {
            stockInfoService.sumVolAndPrice();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }



//    /**
//     * 查询主力资金历史记录
//     * URL：http://localhost:8088/stock/moneyInHistory
//     * @return
//     */
//    @RequestMapping(value = "/moneyInHistory")
//    @ResponseBody
//    public String moneyInHistory(String stockCode) {
//        List list = Lists.newArrayList();
//        try {
//            stockMoneyInTask.moneyInHistory(stockCode);
//        } catch (Exception e) {
//            log.error("CalculationController.count exception={}", e);
//        }
//        return JSONObject.toJSONString(list);
//    }



    /**
     * 修正历史数据
     * @return
     * url：http://127.0.0.1:8080/stock/updateAndFixData?year=2022&season=1
     */
    @RequestMapping(value = "/updateAndFixData")
    public String updateAndFixData() {
        List list = Lists.newArrayList();
        try {
            updateAndFixDataService.excute("2022", "1");
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }

    /**
     *  更新5日、10日、20日的价格和成交量
     * @return
     */
    @RequestMapping(value = "/updtePriceAndVolService")
    @ResponseBody
    public String updtePriceAndVolService() {
        List list = Lists.newArrayList();
        try {
            updtePriceAndVolService.updtePriceAndVol();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }

    /**
     *  更新5日、10日、20日的价格和成交量
     * @return
     * url：http://127.0.0.1:8080/stock/updateZhangDie
     */
    @RequestMapping(value = "/updateZhangDie")
    @ResponseBody
    public String updateZhangDie() {
        List list = Lists.newArrayList();
        try {
            updtePriceAndVolService.updateZhangDie();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }

    /**
     *  更新股票池
     * @return
     * url：http://127.0.0.1:8080/stock/updateCore
     */
    @RequestMapping(value = "/updateCore")
    @ResponseBody
    public String updateCore() {
        List list = Lists.newArrayList();
        try {
           // updateStockCoreService.batchExcute(1);
            updateStockCoreService.updateCore(1);
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }

    @RequestMapping(value = "/aaaaa")
    @ResponseBody
    public String aaaaa() {
        List list = Lists.newArrayList();
        try {
           // updateStockCoreService.batchExcute(1);
            updateStockCoreService.getDataByDfcf();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }



    /**
     *  更新涨跌
     * @return
     * url：http://127.0.0.1:8080/stock/updateZhangDieNew
     */
    @RequestMapping(value = "/updateZhangDieNew")
    @ResponseBody
    public String updateZhangDieNew() {
        List list = Lists.newArrayList();
        try {
            jiSuanZhangDieService.update();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }

    /**
     *  獲取情緒指數
     * @return
     * url：http://127.0.0.1:8080/stock/getZhiShu
     */
    @RequestMapping(value = "/getZhiShu")
    @ResponseBody
    public String getZhiShu() {
        List list = Lists.newArrayList();
        try {
            qinXUZhiShuService.getZhiShu();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }
}

